package com.xiaolun.nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class NIOClient {
	public static void main(String[] args) throws Exception {

		//得到一个网络通道
		SocketChannel socketChannel = SocketChannel.open();
		//设置非阻塞
		socketChannel.configureBlocking(false);
		//提供服务器端的ip 和 端口
		InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 6666);
		//连接服务器，socketChannel.connect(inetSocketAddress连接成功返回true
		if (!socketChannel.connect(inetSocketAddress)) {
			// 连接不成功的提醒语句
			while (!socketChannel.finishConnect()) {
				System.out.println("因为连接需要时间，客户端不会阻塞，可以做其它工作..");
			}
		}

		//如果连接成功，就发送数据
		String str = "hello, 尚硅谷~";
		//Wraps a byte array into a buffer
		ByteBuffer buffer = ByteBuffer.wrap(str.getBytes());
		//发送数据，将 buffer 数据写入 channel
		socketChannel.write(buffer);
		System.in.read();
	}
}
